package com.qf.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect// 声明此类是一个切面类 会包含切入点（pointcut） 和
@Component//声明组件
public class MyAspect {
    //   定义切入点
    @Pointcut("execution(* com.qf.service.UserServiceImpl.*(..))")
    public void pc() {
        System.out.println("哈哈哈");
    }

    @Before("pc()")
    public void mybefore(JoinPoint a) {
        System.out.println("before~~~~");
    }

    @AfterReturning(value = "pc()", returning = "ret")
    public void myAfterReturning(JoinPoint a, Object ret) {
        System.out.println("after~~~~:" + ret);
    }

    @Around("pc()")
    public Object myInterceptor(ProceedingJoinPoint p) throws Throwable {
        System.out.println("interceptor1~~~~");
        Object ret = p.proceed();
        System.out.println("interceptor2~~~~");
        return ret;
    }

    @AfterThrowing(value = "pc()", throwing = "ex")
    public void myThrows(JoinPoint jp, Exception ex) {
        System.out.println("throws");
        System.out.println("====" + ex.getMessage());
    }
}
